/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.informet.ipd.modelo.dao;

import com.informet.ipd.modelo.vo.IndicadorVO;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author LAPTOP01
 */
public class IndicadorDao extends DBase {

    Connection conn = null;

    public IndicadorDao() {
        super();
    }

    private void setIndicador(IndicadorVO bean, ResultSet rs) throws SQLException {
        bean.setNUM_CODINDO_PK(rs.getInt("NUM_CODINDO_PK"));
        bean.setNUM_CODMETAO_PK(rs.getInt("NUM_CODMETAO_PK"));
        bean.setTX_DESCINDO(rs.getString("TX_DESCINDO"));
        bean.setNUM_IDINDO(rs.getInt("NUM_IDINDO"));
        bean.setNUM_IDOBJ(rs.getInt("NUM_IDOBJ"));
        bean.setNUM_IDMOBJ(rs.getInt("NUM_IDMOBJ"));
    }

    public String spMan(int tipo, IndicadorVO bean) {
        String msj = "";
        try {
            conn = DriverManager.getConnection(url, usuario, password);
            CallableStatement cs = conn.prepareCall("{call Man_INDOBJ(?,?,?,?,?)}");
            cs.setInt(1, tipo);
            cs.setInt(2, bean.getNUM_CODINDO_PK());
            cs.setString(3, bean.getTX_DESCINDO());
            cs.setInt(4, bean.getNUM_CODMETAO_PK());
            cs.setInt(5, bean.getNUM_IDINDO());

            cs.execute();



        } catch (Exception e) {
            msj = e.toString();
            return msj;
        }
        return msj;
    }

    public ArrayList<IndicadorVO> selectLista() throws SQLException {

        Statement stmt = null;
        ResultSet rs = null;
        ArrayList<IndicadorVO> lista = new ArrayList<IndicadorVO>(0);
        //lista=null;
        String sql = "";
        try {
            conn = DriverManager.getConnection(url, usuario, password);
            stmt = conn.createStatement();
            sql = "select NUM_CODINDO_PK,TX_DESCINDO from ME_INDOBJ";
            rs = stmt.executeQuery(sql);
            IndicadorVO bean = null;
            while (rs.next()) {
                bean = new IndicadorVO();
                setIndicador(bean, rs);
                lista.add(bean);
            }

        } finally {
            if (rs != null) {
                rs.close();
                rs = null;
            }
            if (stmt != null) {
                stmt.close();
                stmt = null;
            }
            if (conn != null) {
                conn.close();
                conn = null;
            }

        }
        return lista;
    }

    public List getlist() throws SQLException {

        Statement stmt = null;
        ResultSet rs = null;
        ArrayList<IndicadorVO> lista = new ArrayList<IndicadorVO>(0);
        //lista=null;
        String sql = "";
        try {
            conn = DriverManager.getConnection(url, usuario, password);
            stmt = conn.createStatement();
            sql = "select NUM_CODINDO_PK,TX_DESCINDO from ME_INDOBJ";
            rs = stmt.executeQuery(sql);
            IndicadorVO bean = null;
            while (rs.next()) {
                bean = new IndicadorVO();
                setIndicador(bean, rs);
                lista.add(bean);
            }

        } finally {
            if (rs != null) {
                rs.close();
                rs = null;
            }
            if (stmt != null) {
                stmt.close();
                stmt = null;
            }
            if (conn != null) {
                conn.close();
                conn = null;
            }

        }
        return lista;
    }

     public List getlist(int meta) throws SQLException {
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        ArrayList<IndicadorVO> lista = new ArrayList<IndicadorVO>(0);
        //lista=null;
        String sql = "";
        try {
            conn = DriverManager.getConnection(url, usuario, password);
            sql = "select indo.NUM_CODINDO_PK,indo.NUM_IDINDO,indo.TX_DESCINDO,indo.NUM_CODMETAO_PK, obj.NUM_IDOBJ,mobj.NUM_IDMOBJ from ME_METAOBJ mobj inner join ME_OBJ obj on obj.NUM_CODOBJ_PK=mobj.NUM_CODOBJ_PK inner join ME_INDOBJ indo on indo.NUM_CODMETAO_PK=mobj.NUM_CODMETAO_PK where indo.NUM_CODMETAO_PK=? order by NUM_IDINDO";
            pstmt = conn.prepareStatement(sql);
            pstmt.setInt(1, meta);
            rs = pstmt.executeQuery();

            IndicadorVO bean = null;
            while (rs.next()) {
                bean = new IndicadorVO();
                setIndicador(bean, rs);
                lista.add(bean);
            }

        } finally {
            if (rs != null) {
                rs.close();
                rs = null;
            }
            if (pstmt != null) {
                pstmt.close();
                pstmt = null;
            }
            if (conn != null) {
                conn.close();
                conn = null;
            }

        }
        return lista;
    }

    public IndicadorVO selectById(int meta, int id) throws SQLException {
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        Connection conn = null;
        IndicadorVO bean = null;
        try {
            conn = DriverManager.getConnection(url, usuario, password);
            String sql = "select * from ME_INDOBJ where NUM_CODINDO_PK=? and NUM_CODMETAO_PK=?";
            pstmt = conn.prepareStatement(sql);
            pstmt.setInt(1, meta);
            pstmt.setInt(2, id);
            rs = pstmt.executeQuery();
            bean = new IndicadorVO();
            if (rs.next()) {
                setIndicador(bean, rs);
            }
            rs.close();
            pstmt.close();
        } finally {
            if (rs != null) {
                rs.close();
                rs = null;
            }
            if (pstmt != null) {
                pstmt.close();
                pstmt = null;
            }
            if (conn != null) {
                conn.close();
                conn = null;
            }
        }

        return bean;
    }
}

